package com.bdd.controller;


import com.bdd.Result.Result;
import com.bdd.entity.Discount;
import com.bdd.model.bo.CategoryBO;
import com.bdd.model.bo.DiscountBO;
import com.bdd.model.bo.DiscountAddBO;
import com.bdd.model.bo.PageBO;
import com.bdd.service.DiscountService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @author huanghaining
 * @date 2022/3/18
 */
@RestController
@RequestMapping("/discount")
@Api(value = "折扣管理")
public class DisCountController {

    @Autowired
    DiscountService discountService;


    @GetMapping("/list")
    @ResponseBody
    @ApiOperation(value = "折扣列表")
//    @RequiresRoles({"ADMIN"})
    public Result<PageInfo<DiscountBO>>list(PageBO pageBO){
        if(pageBO.getPageNum()<=0){
            pageBO.setPageNum(1);
        }
        if(pageBO.getPageSize()<=0){
            pageBO.setPageSize(10);
        }
        PageInfo<DiscountBO> discountBOList = new PageInfo<>(discountService.list(pageBO.getPageNum(),pageBO.getPageSize()));
        return Result.ok(discountBOList);
    }

    @PostMapping("/add")
    @ResponseBody
    @ApiOperation(value = "增加折扣")
//    @RequiresRoles({"ADMIN"})
    public Result add(DiscountBO bo){
        if (Objects.isNull(bo.getPromotionName())&&Objects.isNull(bo.getDiscounts())){
            throw new IllegalArgumentException("折扣名和折扣价不可为空");
        }
        discountService.add(bo);
        return Result.ok();
    }

    @PostMapping("/edit")
    @ResponseBody
    @ApiOperation(value = "编辑折扣")
//    @RequiresRoles({"ADMIN"})
    public Result edit(DiscountBO bo){
        if (Objects.isNull(bo.getPromotionName())&&Objects.isNull(bo.getDiscounts())){
            throw new IllegalArgumentException("折扣名和折扣价不可为空");
        }
        discountService.edit(bo);
        return Result.ok();
    }

    @PostMapping("/delete")
    @ResponseBody
    @ApiOperation(value = "删除折扣项")
//    @RequiresRoles({"ADMIN"})
    public Result delete(Integer id){
        if(Objects.isNull(id)){
            throw new IllegalArgumentException("折扣id不可为空");
        }
        discountService.delete(id);
        return Result.ok();
    }

    @GetMapping("/searchByName")
    @ResponseBody
    @ApiOperation(value = "根据折扣名称搜索折扣")
//    @RequiresRoles({"ADMIN"})
    public Result<List<DiscountBO>> searchByName(String name){
        if(StringUtils.isBlank(name)){
            throw new IllegalArgumentException("折扣名称不可为空");
        }
        List<DiscountBO> discountBOList = discountService.searchByName(name);
        return Result.ok(discountBOList);
    }

}
